$(function() {
	var url={
		car:"{:url('/adminapi/Car/carList')}",
		order:"{:url('adminapi/Order/orderList')}",
		staff:'{:url("adminapi/Passenger/passengerList")}',
		driver:"{:url('adminapi/Driver/driverList')}"
	};
	var data={
		page:1,
		pagenum:99999,
	}
	data.where_json=JSON.stringify({
		company_id:['in',$('.company_all_id').val()]
	})
	//请求车辆数据
	$.ajax({
		type: "post",
		url: url.car,
		data: data,
		success: function(res) {
			console.log(res)
			//车辆数量
			$('.car_number').html(res.count);
			var mileage = 0;
			var carlist = [];
			var carlist_main = [];
			
			if(res.data.length==0){
			
				$('.car_kilometre').html(`0公里`)
				$('#carUnit').html('该单位无所属车辆')
				$('#carUnit').css('text-align','center')
				$('#carUnit').css('height','50px')
				$('.car_financial').html(`0元`)
				$('#carFinancial').html('该单位无所属车辆')
				$('#carFinancial').css('text-align','center')
				$('#carFinancial').css('height','50px')

			}else{
				
			for(var caridx = 0; caridx < res.data.length; caridx++) {
				mileage += (res.data[caridx].mileage-0)
				carlist.push(res.data[caridx].number)
				if(caridx<10){
					carlist_main.push({
						value: res.data[caridx].mileage,
						name: res.data[caridx].number
					})
				}
			}
			$('.car_kilometre').html(mileage + "公里");
			var carUnit = echarts.init(document.getElementById('carUnit'));
			carUnitoption = {
				tooltip: {
					trigger: 'item',
					formatter: "{a} <br/>{b} : {c} ({d}%)"
				},
				legend: {
					orient: 'vertical',
					left: 'left',
					data: carlist
				},
				series: [{
					name: '访问来源',
					type: 'pie',
					radius: '40%',
					center: ['50%', '40%'],
					data: carlist_main,
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}]
			};
			carUnit.setOption(carUnitoption);
			var carFinancial = echarts.init(document.getElementById('carFinancial'));
			
			carFinancialoption = {

				tooltip: {
					trigger: 'item',
					formatter: "{a} <br/>{b} : {c} ({d}%)"
				},
				legend: {
					orient: 'vertical',
					left: 'left',
					data: ['维修保养', '加油', '保险', '年检']
				},
				series: [{
					name: '访问来源',
					type: 'pie',
					radius: '40%',
					center: ['50%', '40%'],
					data: [{
							value: 335,
							name: '维修保养'
						},
						{
							value: 310,
							name: '加油'
						},
						{
							value: 234,
							name: '保险'
						},
						{
							value: 135,
							name: '年检'
						}
					],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}]
			};
			carFinancial.setOption(carFinancialoption);
			}
		}
	})
	//请求订单数据
	$.ajax({
		type: "post",
		url: url.order,
		data: data,
		success: function(res) {
			
			$('.order_num').html(res.count + '单')
			var orderarr = []
			for(var j = 0; j < 8; j++) {
				if(orderarr[j]) {

				} else {
					orderarr[j] = 0
				}
			}
			for(var i = 0; i < res.data.length; i++) {
				if(res.data[i].use_type) {
					if(orderarr[(res.data[i].use_type - 1)]) {
						orderarr[res.data[i].use_type - 1] = orderarr[res.data[i].use_type - 1]+1
					} else {
						orderarr[(res.data[i].use_type - 1)] = 1
					}
				}
			}
			console.log(orderarr)
			var orderFinancial = echarts.init(document.getElementById('orderFinancial'));
			orderFinancialoption = {
				tooltip: {
					trigger: 'item',
					formatter: "{a} <br/>{b} : {c} ({d}%)"
				},
				legend: {
					orient: 'vertical',
					left: 'left',
					data: ['行政执法 '+orderarr[0]+'单',
					'特殊公务 '+orderarr[1]+'单', 
					'重要会议 '+orderarr[2]+'单', 
					'机要通讯 '+orderarr[3]+'单', 
					'视察调研 '+orderarr[4]+'单', 
					'公务接待 '+orderarr[5]+'单', 
					'应急公务 '+orderarr[6]+'单', 
					'紧急就医 '+orderarr[7]+'单'
					]
				},
				series: [{
					name: '访问来源',
					type: 'pie',
					radius: '40%',
					center: ['50%', '40%'],
					data: [{
							value: orderarr[0],
							name: '行政执法 '+orderarr[0]+'单'
						},
						{
							value: orderarr[1],
							name: '特殊公务 '+ orderarr[1]+'单'
						},
						{
							value: orderarr[2],
							name: '重要会议 '+orderarr[2]+'单'
						},
						{
							value: orderarr[3],
							name: '机要通讯 '+orderarr[3]+'单'
						},
						{
							value: orderarr[4],
							name: '视察调研 '+orderarr[4]+'单'
						},
						{
							value: orderarr[5],
							name: '公务接待 '+orderarr[5]+'单'
						},
						{
							value: orderarr[6],
							name: '应急公务 '+orderarr[6]+'单'
						},
						{
							value: orderarr[7],
							name: '紧急就医 '+orderarr[7]+'单'
						}
					],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}]
			};
			orderFinancial.setOption(orderFinancialoption);
		}
	})
	//请求人员数据
	$.ajax({
		type: "post",
		url: url.staff,
		data: data,
		success: function(res) {
			//人员数量
			$('.staff_number').html(res.count);
		}
	})
	//请求公司基本数据
	$.ajax({
		type: "post",
		url: "{:url('index/getInfo')}",
		data: {},
		success: function(res) {
			console.log(res)
			$("#loading-first").fadeOut(500);
			$('.companyname').html(res.data.company_name);
			if(res.data.prop==1){
				$('.company-type').html('政府机关单位')
			}else if(res.data.prop==2){
				$('.company-type').html('事业单位')
			}else if(res.data.prop==3){
				$('.company-type').html('国营单位')
			}else if(res.data.prop==4){
				$('.company-type').html('民营单位')
				$('.unit_limit').closest('div').remove()
				$('.ticket_financial_limit').closest('div').remove()
				$('.finance').remove()
			}else if(res.data.prop==5){
				$('.company-type').html('公检法')
			}else if(res.data.prop=='1,11'){
				$('.company-type').html('政府机关单位市直')
			}else if(res.data.prop=='1,12'){
				$('.company-type').html('政府机关单位乡镇')
			}else if(res.data.prop=='1,13'){
				$('.company-type').html('政府机关单位四办')
			}
			//可用额度
			let quota = res.data.quota ? res.data.quota-res.data.used_quota : 0;
			let unit_quota = res.data.unit_quota ? res.data.unit_quota-res.data.used_unit_quota : 0;
			$('.financial_limit').html(quota.toFixed(2));
			$('.unit_limit').html(unit_quota.toFixed(2));
			//可开票额度
			$('.ticket_financial_limit').html(res.data.used_quota ? res.data.used_quota : '0.00');
			$('.ticket_unit_limit').html(res.data.used_unit_quota ? res.data.used_unit_quota : '0.00');
			var orderUnit = echarts.init(document.getElementById('orderUnit'));
			orderUnitoption = {
				tooltip: {
					trigger: 'item',
					formatter: "{a} <br/>{b} : {c} ({d}%)"
				},
				legend: {
					orient: 'vertical',
					left: 'left',
					data: ['加班', '商务接特', '出差']
				},
				series: [{
					name: '访问来源',
					type: 'pie',
					radius: '40%',
					center: ['50%', '40%'],
					data: [{
							value: 335,
							name: '加班'
						},
						{
							value: 310,
							name: '商务接特'
						},
						{
							value: 234,
							name: '出差'
						}
					],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowOffsetX: 0,
							shadowColor: 'rgba(0, 0, 0, 0.5)'
						}
					}
				}]
			};

			

			orderUnit.setOption(orderUnitoption);
			

		},
		error: function() {
			$("#loading-first").fadeOut(500);
		}
	});

})